package com.lxg.security.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lxg.security.exception.KaptchaNotMatchException;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author 陆小根
 * date: 2022/04/25
 * Description:
 */

public class LoginKaptchaFilter extends UsernamePasswordAuthenticationFilter {

  public static final String FROM_KAPTCHA_KEY = "kaptcha";

  private String kaptchaParameter = FROM_KAPTCHA_KEY; // 默认值

  public String getKaptchaParameter() {
    return kaptchaParameter;
  }

  public void setKaptchaParameter(String kaptchaParameter) {
    this.kaptchaParameter = kaptchaParameter;
  }

  @Override
  public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
    if (!request.getMethod().equals("POST")) {
      throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
    }
    try {
      // 1.获取请求数据
      Map<String, String> userInfo = new ObjectMapper().readValue(request.getInputStream(), Map.class);
      String kaptcha = userInfo.get(getKaptchaParameter());
      String username = userInfo.get(getUsernameParameter());
      String password = userInfo.get(getPasswordParameter());
      // 2.获取session中验证码
      String sessionVerifyCode = (String) request.getSession().getAttribute("kaptcha");
      if (!ObjectUtils.isEmpty(kaptcha) && !ObjectUtils.isEmpty(sessionVerifyCode)
              && kaptcha.equalsIgnoreCase(sessionVerifyCode)) {
        // 3.获取用户名 和密码认证
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
        setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    throw new KaptchaNotMatchException("验证码不匹配!");
  }
}
